package com.car.web.admin;

import com.car.domain.Admin;
import com.car.server.impl.AdminLogServerImpl;
import org.apache.commons.beanutils.BeanUtilsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.UUID;

/**
 * 注册
 * 要判断用户名字是否相同
 * 相同则输出组册失败
 * 还有判断id
 *
 */
@WebServlet("/log/AdminRegisterWeb")
public class AdminRegisterWeb extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //也是获取封装
        Admin admin = new Admin();
        try {
            new BeanUtilsBean().populate(admin,req.getParameterMap());
            //获取admin
            int i = new AdminLogServerImpl().RegisterAdd(admin);
            if (i==0){
                //失败
                req.setAttribute("register","名字重复请重新选择");
                req.getRequestDispatcher("register.jsp").forward(req,resp);
            }else{
                //成功
                req.getSession().setAttribute("register","注册成功了");
                resp.sendRedirect("log.jsp");
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
